﻿/*
 * @author: wizardc
 */

using System;

namespace Dou.Utils
{
    /// <summary>
    /// 时间日期工具类
    /// </summary>
    public static class TimeUtil
    {
        /// <summary>
        /// 获取指定时间的时间戳
        /// </summary>
        public static long GetTimestamp(DateTime dateTime, bool useMillisecond = false)
        {
            if (useMillisecond)
            {
                return new DateTimeOffset(dateTime).ToUnixTimeMilliseconds();
            }
            return new DateTimeOffset(dateTime).ToUnixTimeSeconds();
        }
        
        /// <summary>
        /// 获取当前时间戳
        /// </summary>
        public static long GetNowTimestamp(bool useMillisecond = false)
        {
            if (useMillisecond)
            {
                return DateTimeOffset.Now.ToUnixTimeMilliseconds();
            }
            return DateTimeOffset.Now.ToUnixTimeSeconds();
        }

        /// <summary>
        /// 时间戳转换为当前时间
        /// </summary>
        public static DateTime GetDate(long timestamp, bool useMillisecond = false)
        {
            var dateTimeOffset = useMillisecond ? DateTimeOffset.FromUnixTimeMilliseconds(timestamp) : DateTimeOffset.FromUnixTimeSeconds(timestamp);
            return dateTimeOffset.LocalDateTime;
        }
        
        /// <summary>
        /// 格式化时间戳
        /// </summary>
        public static string FormatDate(string format, long timestamp, bool useMillisecond = false)
        {
            var dateTime = GetDate(timestamp, useMillisecond);
            return dateTime.ToString(format);
        }

        /// <summary>
        /// 格式化当前时间
        /// </summary>
        public static string FormatNowDate(string format)
        {
            return DateTime.Now.ToString(format);
        }
    }
}
